<style>
  .layui-btn{
    padding: 0 7px !important;
  }
  .layui-laypage a, .layui-laypage span{
    background-color: #f2f2f2;
  }
</style>

<div style="padding-left: 50px;display: none">
  <caption style="margin-left: 40px;"><span >
                <button type="button" class="btn btn-primary layui-btn"  onclick="method5('tableExcel')">
                    <span class="glyphicon glyphicon-folder-open">导出Excel</span>
                </button>
            </span>
  </caption>
</div>
<script type="text/html" id="tol">
  <div>
    <div style="float:left;">分组列表</div>
    <div style="float:right;margin-right: 50px">共有数据：<span id="count"></span>条</div>
  </div>

</script>

    <table class="layui-table" id="groupdata" lay-filter="groupdata">

    </table>


<!--</div>-->
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>
<script type="text/html" id="caozuo">
  <button class="btn layui-btn" lay-event="groupStatusEvent" id="groupStatusButton">
    {{#if(d.group_status=='启用'){ }}
    禁用
    {{# }else{}}
    启用
    {{# }}}
  </button>

  <input type="button"  value="编辑" class="layui-btn " lay-event="updateEvent"/>
  <!--<a href="#/layui/groupedit" class="layui-btn " lay-event="updateEvent">编辑</a>-->
  <input type="button"  value="终端列表" class="layui-btn " lay-event="terminalEvent"/>
  <!--<input type="button"  value="主题管理" class="layui-btn " lay-event="ThemeEvent"/>-->
</script>
<script>

  function GetRequest() {
    var url = decodeURI(location.search); //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
        theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  }

  var json=GetRequest();

  var pages= localStorage.getItem('page');


  var local_store = layui.data("local_store");
  var local = local_store.token;
  $.ajax({
      type:"get",
      url:"/admin/station/query/stationLoginQuanxXian?stationId="+json.id,
      dataType: "json",
      contentType:"application/json;charset=utf-8",
      // beforeSend: function(request) {
      //     request.setRequestHeader("token", local);
      // },
      success:function (res) {
          if(res.code==500){
              alert("权限不足")
              location="../../admin/main.html";
              return
          }
      }
  })

    var $ = layui.$;
    var laydate = layui.laydate;
    var form = layui.form;
    var table = layui.table;

    table.render({
      elem: "#groupdata",
      url: "/admin/group/query/groupAll?groupStationId="+json.id,
      id: "t",
      type:'get',
      toolbar:"#tol",
      cols: [[{
        title: "分组编号",
        field: "group_id",
        width: "17%",
        align: "center"
      },
        {
          title: "分组名称",
          field: "group_name",
          width: "17%",
          align: "center"
        }
        ,   {
          title: "所属站点",
          field: "station_name",
          width: "17%",
          align: "center"
        }
        ,
        {
          title: "状态",
          field: "group_status",
          width: "17%",
          align: "center",
          templet:  "<div id='group_status_div'>{{d.group_status}}</div>"
        },
        {
          title: "操作",
          width: "31%",
          align: "center",
          toolbar: "#caozuo"
        }
      ]],
      page:true,
        limit:40,
      done: function(res, curr, count){
        localStorage.setItem('page',curr);
        $("#count").html(count);
      }
    })

    //    删除的监听事件
    table.on("tool(groupdata)", function (obj) {
      var getdata = obj.data; //获得当前行数据
      var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

      switch (layEvent) {
        case "groupStatusEvent":

            var json_data=JSON.stringify(getdata);
            var old = getdata.group_status;
            var newStatus = "禁用";
            if (old=="禁用")
            {
                newStatus = "启用";
            }
            //alert(newStatus);
            var friend={
                "groupId":getdata.group_id,
                "groupStatus": newStatus
            }

            $.ajax({
                url: "/admin/group/update/groupById",
                type: 'put',
                dataType: "json",
                contentType:"application/json;charset=utf-8",
                data: JSON.stringify(friend),
                // beforeSend: function () {
                //     this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']});
                // },
                success: function (res) {
                    if (res.code == 500) {
                        layer.msg("修改失败", {icon: 5});//失败的表情
                        return;
                    } else if (res.code == 200) {

                        var message = $.trim(tr.find("button:last").html());

                        if(message=="启用"){
                            tr.find("td:eq(3)").html("启用");
                            tr.find("button:first-child").html("禁用");
                        }else{
                            tr.find("td:eq(3)").html("禁用");
                            tr.find("button:first-child").html("启用");
                        }

                        layer.msg("修改成功", {
                            icon: 6,//成功的表情
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function () {


                        });
                    }
                },
                complete: function () {
                    layer.close(this.layerIndex);
                },
            });
            return false;//阻止表单跳转
          break;
        case "updateEvent":
          templet:{
            localStorage.setItem('group_Id',getdata.group_id);
            localStorage.setItem('group_Name',getdata.group_name);
            localStorage.setItem('group_status',getdata.group_status);

            if(location.href.indexOf("#reloaded")==-1){
              location='#/layui/groupedit';
              location.reload();
            }
          }

          break;
        case "terminalEvent":
          layui.data("s",{key:"group",value:getdata});
          layer.open({
            title:"终端列表",
            type:2,
            content:'/admin/station_config/views/layui/group_terminal_list.html',
            area:['1200px','600px']
          });

            templet:{
              localStorage.setItem('groupStationId',json.id);

              // if(location.href.indexOf("#reloaded")==-1){
              //   location='#/layui/groupterminal';
              //   location.reload();
              // }
            }
          break;
      }
    });

  /**
   * 设置cookie
   * @param name
   * @param value
   * @constructor
   */
  function Setcookie (name, value)
  {
    document.cookie = name+"="+value+";path=#/layui/groupedit";

    //即document.cookie= name+"="+value+";path=/";   时间可以不要，但路径(path)必须要填写，因为JS的默认路径是当前  页，如果不填，此cookie只在当前页面生效！~
  }
</script>

